package com.example.truthordare;

import java.util.ArrayList;
import java.util.zip.Inflater;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class NameAdapter extends BaseAdapter {
	private Context context;
	private static final String TAG = NameAdapter.class.getSimpleName();
	ArrayList<Player> listArray;

	// private final String[] values;
	public NameAdapter(Context context, ArrayList<Player> listArray) {
		super();
		this.context = context;
		this.listArray = listArray;

	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub

		return listArray.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		Log.i("poss", "pos " + position);
		return position;
	}

	@Override
	public View getView(int position, View view, ViewGroup parent) {
		Log.i("nameadpater", "View");
		if (view == null) {
			LayoutInflater inflater = LayoutInflater.from(parent.getContext());
			view = inflater.inflate(R.layout.row, parent, false);
		}
		final Player player = listArray.get(position);
		TextView textView = (TextView) view.findViewById(R.id.name);
		textView.setText(player.getNameplayer());
		textView.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {

				Log.d(TAG, "string: " + player.getNameplayer());

				Toast.makeText(context, "button clicked: " + player.getId(),
						Toast.LENGTH_SHORT).show();
				final String send = "" + player.getId();
				Bundle sendBundle = new Bundle();
				sendBundle.putString("id", send);
				sendBundle.putString("name", "" + player.getNameplayer());
				Intent i = new Intent(context, Addplayer.class);
				i.putExtras(sendBundle);
				((MainActivity) view.getContext()).startActivityForResult(i,
						1002);

			}

		});
		return view;

	}
}
